{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "acd224f8",
   "metadata": {},
   "source": [
    "### One-hot 编码"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97c76455",
   "metadata": {},
   "source": [
    "优点：操作简单，容易理解\n",
    "\n",
    "缺点：完全割裂了词与词的联系，而且在大语料集下，占据大量内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "824f144c",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e01046b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "from tensorflow.keras.preprocessing.text import Tokenizer\n",
    "\n",
    "vocab = {'周杰伦', '陈奕迅', '王力宏', '李宗盛', '吴亦凡', '鹿晗'}\n",
    "\n",
    "t = Tokenizer(num_words=None, char_level=False)\n",
    "t.fit_on_texts(vocab)\n",
    "\n",
    "for token in vocab:\n",
    "    zero_list = [0]*len(vocab)\n",
    "    # 使用词汇器映射转化现有文本数据，每个词汇对应从1开始的自然数\n",
    "    # 返回样式如：[[2]] 取出其中的数字需要使用[0][0]\n",
    "    token_index = t.texts_to_sequences([token])[0][0] - 1\n",
    "    zero_list[token_index] = 1\n",
    "    print(token, '的one-hot编码为：', zero_list)\n",
    "    \n",
    "# 使用joblib工具保存映射器\n",
    "tokenizer_path = 'data/Tokenizer'\n",
    "joblib.dump(t, tokenizer_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57f658c8",
   "metadata": {},
   "source": [
    "### word2vec"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f45eb880",
   "metadata": {},
   "source": [
    "使用fasttext工具实现word2vec的训练和使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "923ba258",
   "metadata": {},
   "source": [
    "#### 1. CBOW(Continuous bag of words)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41f2fc81",
   "metadata": {},
   "source": [
    "![](./pics/CBOW.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63b3d187",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 获取训练数据\n",
    "!wget -c http://mattmahoney.net/dc/enwik9.zip -P data\n",
    "\n",
    "!unzip data/enwik9.zip -d data/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d629dbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 训练词向量\n",
    "import fasttext\n",
    "model = fasttext.train_unsupervised('data/fil9')  # 默认为skipgram, 100维，5个epoch，12个线程\n",
    "\n",
    "# model = fasttext.train_unsupervised('data/fil9', 'cbow', dim=300, epoch=1, lr=0.1, thread=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7b405ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 查看单词对应的词向量\n",
    "model.get_word_vector('the')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f953b4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 模型效果检验\n",
    "\n",
    "model.get_nearest_neighbors('sports')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cc8ac05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 模型的保存与加载\n",
    "\n",
    "model.save_model('model/fil9.bin')\n",
    "\n",
    "model = fasttext.load_model('model/fil8.bin')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d827704b",
   "metadata": {},
   "source": [
    "#### 2. skipgram"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09e8a698",
   "metadata": {},
   "source": [
    "![](./pics/skipgram.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff20603a",
   "metadata": {},
   "source": [
    "### Word Embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1e4c2177",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import fileinput\n",
    "from torch.utils.tensorboard import SummaryWriter\n",
    "\n",
    "# 实例化一个摘要写入对象\n",
    "writer = SummaryWriter()\n",
    "\n",
    "embedded = torch.randn(100, 50)  # 将其视作已经得到的词嵌入矩阵\n",
    "meta = list(map(lambda x: x.strip(), fileinput.FileInput('data/vocab100.csv')))  # 导入事先准备好的100个中文词汇文件\n",
    "\n",
    "writer.add_embedding(embedded, metadata=meta)\n",
    "writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d0569d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torchX",
   "language": "python",
   "name": "torchx"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
